package org.framework.edge.builder.infrastructure.persistence;

import org.framework.edge.builder.infrastructure.entity.DockerRepositoryBackupRecordDO;
import org.framework.edge.builder.infrastructure.converter.DockerRepositoryBackupRecordConverter;
import org.framework.edge.builder.infrastructure.mapper.DockerRepositoryBackupRecordMapper;
import org.framework.edge.builder.domain.model.docker.repository.backup.record.DockerRepositoryBackupRecordRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.framework.edge.builder.domain.model.docker.repository.backup.record.DockerRepositoryBackupRecord;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe Docker仓库备份记录
 *
 * @author Jia wei Wu
 * @date 2025/07/10 08:18 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class DockerRepositoryBackupRecordRepositoryImpl implements DockerRepositoryBackupRecordRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecord 新增Docker仓库备份记录
     * @return {@link Result<DockerRepositoryBackupRecord>} Docker仓库备份记录新增后领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackupRecord> story(DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        lazyLambdaStream.upsert(dockerRepositoryBackupRecordDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecordList 批量新增Docker仓库备份记录
     * @return {@link Result<List<DockerRepositoryBackupRecord>>} Docker仓库备份记录新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<List<DockerRepositoryBackupRecord>> batchStory(List<DockerRepositoryBackupRecord> dockerRepositoryBackupRecordList) {
        List<DockerRepositoryBackupRecordDO> dockerRepositoryBackupRecordDOList = dockerRepositoryBackupRecordList.stream().map(DockerRepositoryBackupRecordConverter.INSTANCE::fromDockerRepositoryBackupRecord).collect(Collectors.toList());
        lazyLambdaStream.upsert(dockerRepositoryBackupRecordDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecord 查询单个Docker仓库备份记录
     * @return {@link Result<DockerRepositoryBackupRecord>} Docker仓库备份记录领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackupRecord> findOne(DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        DockerRepositoryBackupRecord dockerRepositoryBackupRecordOne = lazyLambdaStream.selectOne(
                LazyWrappers.<DockerRepositoryBackupRecordDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupId, dockerRepositoryBackupRecordDO.getBackupId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupSuccess, dockerRepositoryBackupRecordDO.getBackupSuccess())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getCreateTime, dockerRepositoryBackupRecordDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getId, dockerRepositoryBackupRecordDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageName, dockerRepositoryBackupRecordDO.getSourceImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageVersion, dockerRepositoryBackupRecordDO.getSourceImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceRepositoryId, dockerRepositoryBackupRecordDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTagRepositoryId, dockerRepositoryBackupRecordDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageName, dockerRepositoryBackupRecordDO.getTargetImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageVersion, dockerRepositoryBackupRecordDO.getTargetImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getUpdateTime, dockerRepositoryBackupRecordDO.getUpdateTime())
                , DockerRepositoryBackupRecord.class);
        return ResultFactory.successOf(dockerRepositoryBackupRecordOne);
    }

    /**
     * describe 查询多个Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecord 查询多个Docker仓库备份记录
     * @return {@link Result<List<DockerRepositoryBackupRecord>>} Docker仓库备份记录领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<List<DockerRepositoryBackupRecord>> findList(DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        List<DockerRepositoryBackupRecord> dockerRepositoryBackupRecordList = lazyLambdaStream.selectList(
                LazyWrappers.<DockerRepositoryBackupRecordDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupId, dockerRepositoryBackupRecordDO.getBackupId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupSuccess, dockerRepositoryBackupRecordDO.getBackupSuccess())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getCreateTime, dockerRepositoryBackupRecordDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getId, dockerRepositoryBackupRecordDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageName, dockerRepositoryBackupRecordDO.getSourceImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageVersion, dockerRepositoryBackupRecordDO.getSourceImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceRepositoryId, dockerRepositoryBackupRecordDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTagRepositoryId, dockerRepositoryBackupRecordDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageName, dockerRepositoryBackupRecordDO.getTargetImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageVersion, dockerRepositoryBackupRecordDO.getTargetImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getUpdateTime, dockerRepositoryBackupRecordDO.getUpdateTime())
                , DockerRepositoryBackupRecord.class);
        return ResultFactory.successOf(dockerRepositoryBackupRecordList);
    }

    /**
     * describe 分页查询多个Docker仓库备份记录
     *
     * @param size                         当前页数
     * @param current                      当前页
     * @param dockerRepositoryBackupRecord 分页查询多个Docker仓库备份记录
     * @return {@link Result<LazyPage<DockerRepositoryBackupRecord>>} 分页Docker仓库备份记录领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<LazyPage<DockerRepositoryBackupRecord>> findPage(int size, int current, DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        LazyPage<DockerRepositoryBackupRecord> lazyPage = new LazyPage<>(current, size);
        LazyPage<DockerRepositoryBackupRecord> dockerRepositoryBackupRecordLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<DockerRepositoryBackupRecordDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupId, dockerRepositoryBackupRecordDO.getBackupId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupSuccess, dockerRepositoryBackupRecordDO.getBackupSuccess())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getCreateTime, dockerRepositoryBackupRecordDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getId, dockerRepositoryBackupRecordDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageName, dockerRepositoryBackupRecordDO.getSourceImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageVersion, dockerRepositoryBackupRecordDO.getSourceImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceRepositoryId, dockerRepositoryBackupRecordDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTagRepositoryId, dockerRepositoryBackupRecordDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageName, dockerRepositoryBackupRecordDO.getTargetImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageVersion, dockerRepositoryBackupRecordDO.getTargetImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getUpdateTime, dockerRepositoryBackupRecordDO.getUpdateTime())
                , lazyPage, DockerRepositoryBackupRecord.class);
        return ResultFactory.successOf(dockerRepositoryBackupRecordLazyPage);
    }

    /**
     * describe 删除Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecord 删除Docker仓库备份记录
     * @return {@link Result<DockerRepositoryBackupRecord>} Docker仓库备份记录
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackupRecord> remove(DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        lazyLambdaStream.delete(
                LazyWrappers.<DockerRepositoryBackupRecordDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupId, dockerRepositoryBackupRecordDO.getBackupId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupSuccess, dockerRepositoryBackupRecordDO.getBackupSuccess())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getCreateTime, dockerRepositoryBackupRecordDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getId, dockerRepositoryBackupRecordDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageName, dockerRepositoryBackupRecordDO.getSourceImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageVersion, dockerRepositoryBackupRecordDO.getSourceImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceRepositoryId, dockerRepositoryBackupRecordDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTagRepositoryId, dockerRepositoryBackupRecordDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageName, dockerRepositoryBackupRecordDO.getTargetImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageVersion, dockerRepositoryBackupRecordDO.getTargetImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getUpdateTime, dockerRepositoryBackupRecordDO.getUpdateTime())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在Docker仓库备份记录
     *
     * @param dockerRepositoryBackupRecord Docker仓库备份记录领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<Boolean> exists(DockerRepositoryBackupRecord dockerRepositoryBackupRecord) {
        DockerRepositoryBackupRecordDO dockerRepositoryBackupRecordDO = DockerRepositoryBackupRecordConverter.INSTANCE.fromDockerRepositoryBackupRecord(dockerRepositoryBackupRecord);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<DockerRepositoryBackupRecordDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupId, dockerRepositoryBackupRecordDO.getBackupId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getBackupSuccess, dockerRepositoryBackupRecordDO.getBackupSuccess())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getCreateTime, dockerRepositoryBackupRecordDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getId, dockerRepositoryBackupRecordDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageName, dockerRepositoryBackupRecordDO.getSourceImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceImageVersion, dockerRepositoryBackupRecordDO.getSourceImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getSourceRepositoryId, dockerRepositoryBackupRecordDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTagRepositoryId, dockerRepositoryBackupRecordDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageName, dockerRepositoryBackupRecordDO.getTargetImageName())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getTargetImageVersion, dockerRepositoryBackupRecordDO.getTargetImageVersion())
                        .eqIgnoreEmpty(DockerRepositoryBackupRecordDO::getUpdateTime, dockerRepositoryBackupRecordDO.getUpdateTime())
        );
        return ResultFactory.successOf(exists);
    }

}